package com.bs.service;

import com.bs.domain.FilebeatConfig;
import com.bs.repository.FilebeatConfigRepository;
import com.bs.service.util.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.*;

/**
 * Author: wujian
 * Time: 2019/01/17
 * Desc:
 */
@Service
public class FilebeatConfigService {

    private static final String LOCAL_PATH = "/BSMesWare/logcenter/filebeat/";
    private static final String SERVER_PATH = "/BSMesWare/filebeat/";
    private static final String FILE_NAME = "filebeat_redis.yml";

    @Autowired
    private FilebeatConfigRepository filebeatConfigRepository;

    public List<FilebeatConfig> findAll() {
        return filebeatConfigRepository.findAll();
    }

    public FilebeatConfig save(FilebeatConfig filebeatConfig) {

        return filebeatConfigRepository.save(filebeatConfig);
    }

    public void delete(Long id) {
        filebeatConfigRepository.delete(id);
    }

    public void upload(FilebeatConfig filebeatConfig) {
        String middleware = "";
        switch (filebeatConfig.getMiddleware()) {
            case "ixt":
                middleware = "ixtop";
                break;
            case "tux":
                middleware = "tuxedo";
                break;
            default:
                break;
        }
        Map<String, Object> map1 = new LinkedHashMap<>();
        List<Map<String, Object>> list1 = new ArrayList<>();
        Map<String, Object> map2A = new LinkedHashMap<>();
        map2A.put("input_type", "log");
        List<Object> map2aPathsList = new ArrayList<>();
        map2aPathsList.add("/BSMesWare/"+
            filebeatConfig.getPartitionEname()+
            "/Build/" +
            filebeatConfig.getMiddleware() +
            filebeatConfig.getServerDB() +
            "/Trace/*.*.*");
        map2aPathsList.add("/BSMesWare/" +
            filebeatConfig.getPartitionEname() +
            "/Build/" +
            filebeatConfig.getMiddleware() +
            filebeatConfig.getServerDB() +
            "/Trace.1/*.*.*");
        map2A.put("paths", map2aPathsList);
        map2A.put("encoding", "gb2312");
        Map<String, String> map2Aa = new LinkedHashMap<>();
        map2Aa.put("logtype", "trace");
        map2Aa.put("partition", filebeatConfig.getPartitionEname());
        map2A.put("fields", map2Aa);
        map2A.put("fields_under_root", true);
        map2A.put("multiline.pattern", "^<LOG_START>");
        map2A.put("multiline.negate", true);
        map2A.put("multiline.match", "after");
        map2A.put("multiline.max_lines", 9000);
        map2A.put("close_removed", true);
        list1.add(map2A);
        map1.put("filebeat.prospectors", list1);

        Map<String, Object> map2B = new LinkedHashMap<>();
        map2B.put("input_type", "log");
        List<Object> map2bPathsList = new ArrayList<>();
        map2bPathsList.add("/BSMesWare/" +
            filebeatConfig.getPartitionEname() +
            "/Deployed/s.*.log.*");
        map2B.put("paths", map2bPathsList);
        map2B.put("encoding", "gbk");
        map2B.put("exclude_lines", new String[]{"^<LOG_START>N/A"});
        Map<String, String> map2Ba = new LinkedHashMap<>();
        map2Ba.put("logtype", "deploy");
        map2Ba.put("partition", filebeatConfig.getPartitionEname());
        map2B.put("fields", map2Ba);
        map2B.put("fields_under_root", true);
        map2B.put("multiline.pattern", "^<LOG_START>");
        map2B.put("multiline.negate", true);
        map2B.put("multiline.match", "after");
        map2B.put("multiline.timeout", "100s");
        map2B.put("close_removed", true);
        list1.add(map2B);

        Map<String, Object> map2C = new LinkedHashMap<>();
        map2C.put("input_type", "log");
        List<Object> map2cPathsList = new ArrayList<>();
        map2cPathsList.add("/" +
            filebeatConfig.getXcomVersion() +
            "/log/edsend/*.log");
        map2cPathsList.add("/" +
            filebeatConfig.getXcomVersion() +
            "/log/edrecv/*.log");
        map2cPathsList.add("/" +
            filebeatConfig.getXcomVersion() +
            "/log/session/*");
        map2C.put("paths", map2cPathsList);
        map2C.put("encoding", "gb2312");
        List<String> includeLinesListC = new ArrayList<>();
        includeLinesListC.add("Routing-async");
        map2C.put("include_lines", includeLinesListC);
        Map<String, String> map2Ca = new LinkedHashMap<>();
        map2Ca.put("logtype", "xcom");
        map2Ca.put("partition", filebeatConfig.getPartitionEname());
        map2C.put("fields", map2Ca);
        map2C.put("fields_under_root", true);
        map2C.put("close_renamed", true);
        map2C.put("close_removed", true);
        list1.add(map2C);

        Map<String, Object> map2D = new LinkedHashMap<>();
        map2D.put("input_type", "log");
        List<Object> map2dPathsList = new ArrayList<>();
        map2dPathsList.add("/BSMesWare/Apache/logs/error_log.*");
        map2D.put("paths", map2dPathsList);
        Map<String, String> map2Da = new LinkedHashMap<>();
        map2Da.put("logtype", "apache");
        map2Da.put("partition", filebeatConfig.getPartitionEname());
        map2D.put("fields", map2Da);
        map2D.put("fields_under_root", true);
        list1.add(map2D);

        Map<String, Object> map2E = new LinkedHashMap<>();
        map2E.put("input_type", "log");
        List<Object> map2ePathsList = new ArrayList<>();
        map2ePathsList.add("/BSMesWare/" +
            filebeatConfig.getPartitionEname() +
            "/Build/" +
            filebeatConfig.getMiddleware() +
            filebeatConfig.getServerDB() +
            middleware +
            "/Ulog/ULOG.*");
        map2E.put("paths", map2ePathsList);
        Map<String, String> map2Ea = new LinkedHashMap<>();
        map2Ea.put("logtype", "ixtop");
        map2Ea.put("partition", filebeatConfig.getPartitionEname());
        map2E.put("fields", map2Ea);
        map2E.put("fields_under_root", true);
        map2E.put("multiline.pattern", "^\\d{8}");
        map2E.put("multiline.negate", true);
        map2E.put("multiline.match", "after");
        map2E.put("ignore_older", "48h");
        list1.add(map2E);
        map1.put("filebeat.prospectors", list1);

        Map<String, Object> map2F = new LinkedHashMap<>();
        List<String> hostList = new ArrayList<>();
        hostList.add(filebeatConfig.getRedisHost());
        map2F.put("hosts", hostList);
        map2F.put("key", "filebeat");
        map1.put("output.redis", map2F);
        map1.put("logging.to_files", true);

        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(options);
        try {
            File file = new File(LOCAL_PATH);
            if(!file.exists()){
                file.mkdirs();
            }
            yaml.dump(map1, new FileWriter(LOCAL_PATH + filebeatConfig.getIp()+"_"+FILE_NAME));
            FtpUtil.upload(filebeatConfig.getIp(),
                filebeatConfig.getPort(),
                filebeatConfig.getUsername(),
                filebeatConfig.getPassword(),
                SERVER_PATH,
                LOCAL_PATH,
                FILE_NAME,
                filebeatConfig.getIp()+"_"+FILE_NAME);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
